package com.qf.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.qf.ssm.dto.R;
import com.qf.ssm.pojo.Categories;
import com.qf.ssm.service.CategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/categories")
public class CategoriesController {

    private CategoriesService categoriesService;

    @Autowired
    public void setCategoriesService(CategoriesService categoriesService) {
        this.categoriesService = categoriesService;

    }

    @RequestMapping("category")
    public R addCategories(@RequestBody Categories categories)throws Exception{
        categoriesService.addCategories(categories);
        return R.setOK(categories);
    }

    @GetMapping("/categories")
    public R findAllCategories(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, @RequestParam(required = false) String categoryname,  @RequestParam(defaultValue = "-100") String status) {
        PageInfo<Categories> pageInfo = categoriesService.findAllCategoriesByPage(page, limit,categoryname,status);
        return R.setOK(pageInfo);
    }

    @DeleteMapping("/categories")
    public R deleteCategoriesByIds(Long[] ids){
        categoriesService.deleteCategoriesByIds(ids);
        return R.setOK();
    }
    @PutMapping("/category")
    public R updateCategories(@RequestBody Categories categories){
        categoriesService.updateCategories(categories);
        return R.setOK();
    }



}
